package com.herui.sdyu_lib.utils;

import android.os.Environment;

import com.herui.sdyu_lib.base.BaseAppcation;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Map;


/**
 * Created by Administrator on 2016/5/10.
 */
public class FileUtils {

    /**
     * 通过Context.getExternalFilesDir()方法可以获取到 SDCard/Android/data/你的应用的包名/files/ 目录
     *
     * @param type Environment.DIRECTORY_DOWNLOADS 等等这种命名
     * @return
     */
    public static String getExternalFilesDir(String type) {
        if (isExistExternalStore()) {
            return BaseAppcation.getInstance().getExternalFilesDir(type).getPath();
        }
        return null;
    }

    /**
     * 通过Context.getExternalCacheDir()方法可以获取到 SDCard/Android/data/你的应用包名/cache/目录
     *
     * @return
     */
    public static String getExternalCacheDir() {
        if (isExistExternalStore()) {
            return BaseAppcation.getInstance().getExternalCacheDir().getPath();
        }
        return null;
    }

    /**
     * 内部缓存文件夹下
     *
     * @return
     */
    public static String getCacheDir() {
        return BaseAppcation.getInstance().getCacheDir().getPath();
    }

    /**
     * sd卡路径
     *
     * @return
     */
    public static String getExternalStorePath() {
        if (isExistExternalStore()) {
            return Environment.getExternalStorageDirectory().getAbsolutePath();
        }
        return null;
    }

    /**
     * 是否有外存卡
     *
     * @return
     */
    public static boolean isExistExternalStore() {
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 读取配置文件信息
     * @param filePath
     * @param configs   保存配置文件信息
     * @throws IOException
     */
    public static void readConfigFile(String filePath, Map<String, String> configs) throws
            IOException {
        FileReader reader = new FileReader(filePath);
        BufferedReader br = new BufferedReader(reader);

        String str = null;
        while ((str = br.readLine()) != null) {
            String[] strings = str.split("=");
            if (strings != null && strings.length == 2) {
                if (configs.get(strings[0]) != null)
                    configs.put(strings[0], strings[1]);
            }
        }
        br.close();
        reader.close();
    }

    /**
     * 读取文件转化为string
     * @param filePath
     * @return
     * @throws IOException
     */
    public static String readFile(String filePath) throws IOException {

        Reader reader = new FileReader(filePath);
        char[] chars = new char[1024];
        StringBuilder builder = new StringBuilder();
        int len = reader.read(chars);
        while (len > 0){
            builder.append(new String(chars, 0, len));
            len = reader.read(chars);
        }
        reader.close();
        return builder.toString();
    }

    /**
     * 计算语音文件的时间长度
     *
     * @param file
     * @return
     */
    public static int calculateVoiceTime(String file) {
        File _file = new File(file);
        if (!_file.exists()) {
            return 0;
        }
        // 650个字节就是1s
        int duration = (int) Math.ceil(_file.length() / 650);
        if (duration > 60) {
            return 60;
        }
        if (duration < 1) {
            return 1;
        }
        return duration;
    }

    /**
     * Java文件操作 获取文件扩展名 Get the file extension, if no extension or file name
     *
     */
    public static String getExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return "";
    }

    /**
     * Java文件操作 获取不带扩展名的文件名
     */
    public static String getFileNameNoEx(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length()))) {
                return filename.substring(0, dot);
            }
        }
        return filename;
    }

    /**
     * 返回文件名
     *
     * @param pathName
     * @return
     */
    public static String getFilename(String pathName) {
        File file = new File(pathName);
        if (!file.exists()) {
            return "";
        }
        return file.getName();
    }

}
